﻿using System;
using System.Collections.Generic;

using Gonte.UI;
using Gonte.Wpf.Controls.ViewModel;

using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace MyWpfControlsTest
{
    [TestClass()]
    public class ObjectViewModelTest
    {
        private TestContext testContextInstance;

        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        [UserInterface(LabelProperty = "Name")]
        [UserInterfaceItem(PropertyName = "Children")]
        public class ParentModel
        {
            private String name_;

            public String Name
            {
                get { return name_; }
                set { name_ = value; }
            }

            private List<ChildModel> children_;

            public List<ChildModel> Children
            {
                get { return children_; }
                set { children_ = value; }
            }
        }

        [UserInterface(LabelProperty = "Name")]
        public class ChildModel
        {
            private String name_;

            public String Name
            {
                get { return name_; }
                set { name_ = value; }
            }
        }

        [TestMethod()]
        public void ObjectViewModelConstructorTest()
        {
            ParentModel parent = new ParentModel();
            parent.Name = "Parent";
            parent.Children = new List<ChildModel>();

            ChildModel child1 = new ChildModel();
            child1.Name = "Child1";
            parent.Children.Add(child1);

            ChildModel child2 = new ChildModel();
            child2.Name = "Child2";
            parent.Children.Add(child2);

            ObjectViewModel viewModel = new ObjectViewModel(null, parent);

            Assert.AreEqual(parent, viewModel.Model);
            Assert.AreEqual(parent.Name, viewModel.Label);
            Assert.AreEqual(parent.Children.Count, viewModel.Children.Count);

            ChildModel child3 = new ChildModel();
            child3.Name = "Child3";
            //parent.Children.Add(child3); No need to add it. The view model will do it

            viewModel.AddChild(child3);
            Assert.AreEqual(parent.Children.Count, viewModel.Children.Count);

            viewModel.MoveChildUp(child3);
            Assert.AreEqual(child3, parent.Children[1]); // Verify it is in the middle
            Assert.AreEqual(parent.Children.Count, viewModel.Children.Count);

            viewModel.MoveChildDown(child1);
            Assert.AreEqual(child1, parent.Children[1]); // Verify it is in the middle
            Assert.AreEqual(parent.Children.Count, viewModel.Children.Count);

            viewModel.RemoveChild(child1);
            Assert.AreEqual(parent.Children.Count, viewModel.Children.Count);
        }
    }
}
